/*
 *  Singleton.h
 *  RTDemo
 *
 *  Created by steveca  on 04/05/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */

#ifndef __SINGLETON_H__
#define __SINGLETON_H__

#include <assert.h>

namespace RTRT 
{
    template <typename T>
    class Singleton 
        {
        public:
            Singleton( void )
            {
                assert( !ms_singleton );
#if defined( _MSC_VER ) && _MSC_VER < 1200
                int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
                ms_singleton = (T*)((int)this + offset);
#elses
                ms_singleton = static_cast< T* >( this );
#endif
            }
            
            ~Singleton( void )
            { 
                assert( ms_singleton );  ms_singleton = 0;  
            }
    
            static T& getSingleton( void )
            {
                assert( ms_singleton );  return ( *ms_singleton ); 
            }
            
            static T* getSingletonPtr( void )
            {
                return ms_singleton; 
            }
            
        protected:
            T *ms_singleton;
        };
}

#endif
